<?php
namespace osc\api\controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19
 * Time: 14:18
 */

use think\Db;
use think\Request;

class User extends Base
{


    private $member;

    public function __construct()
    {
        parent::__construct();
        //用户模型
        $this->member       =   new \osc\api\model\Member();

    }

  
    /**
     * 获取用户个人信息
     * @return string
     */
    public function index(){

        if($this->request->isPost()){
            $x_uid = $this->request->post('uid');

            if(!$x_uid){
                return json_encode(['check'=>0, 'mage'=>'提交错误!']);
            }

            //解密
            $uid = think_ucenter_decrypt($x_uid,config('PWD_KEY'));

            $trade = $this->member->where('uid',$uid)->find();

            if(!$trade){
                return json_encode(['check'=>-1, 'mage'=>'用户不存在!']);
            }

            $data = [
                'user' => [
                    'uid'               => $x_uid,
                    'username'          => $trade['username'],
                    'nickname'          => $trade['nickname'],
                    'idcardname'        => $trade['idcardname'],
                    'idcard'            => $trade['idcard'],
                    'area'              => $trade['area'],
                    'credit_score'      => $trade['credit_score']
                ],

                'menu'  => [
                    [
                        'name' => '修改登录密码',
                        'link' => 'api/user/updateuserpasswd'
                    ],
                    [
                        'name' => '修改二级密码',
                        'link' => 'api/user/updateuser2passwd'
                    ],
                    [
                        'name' => '交易所账号管理',
                        'link' => 'api/user/exchange_trade'
                    ]
                ]
            ];

            return json_encode(['check'=>1, 'mage'=>'OK', 'data'=>$data]);
        }else{
            return json_encode(['check'=>0, 'mage'=>'提交错误!']);
        }
    }


    /**
     * 修改密码
     * @return array|mixed
     */
    function update_psd()
    {
        if(request()->isPost()){
            $data=input('post.');

            if(empty($data['oldpassword'])){
                return json_encode(['check'=>0, 'mage'=>'请填写旧密码!']);
            }

            if(empty($data['password'])){
                return json_encode(['check'=>0, 'mage'=>'请填写新密码!']);
            }

            if(empty($data['pwd2'])){
                return json_encode(['check'=>0, 'mage'=>'请填写确认新密码!']);
            }

//            var_dump($data);exit;
            $r=osc_service('common','Member')->api_updateuserpasswd($data);
            if(isset($r['error'])){
                return json_encode(['check'=>0, 'mage'=>$r['error']]);
            }
            return json_encode(['check'=>1, 'mage'=>'密码修改成功']);

        }else{
            return json_encode(['check'=>0, 'mage'=>'提交错误!']);
        }
    }

}